package org.jeecg.modules.bobo.common.aspect;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.jeecg.common.util.IPUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.bobo.common.aspect.annotation.LbAutoLog;
import org.jeecg.modules.bobo.common.entity.LbAutoServiceLog;
import org.jeecg.modules.bobo.common.exception.BoboException;
import org.jeecg.modules.bobo.common.service.ILbAutoServiceLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;


/**
 * 系统日志，切面处理类
 *
 * @Author scott
 * @email jeecgos@163.com
 * @Date 2018年1月14日
 */

@Aspect
@Component
public class LbAutoLogAspect {
	@Autowired
	private ILbAutoServiceLogService autoServiceLogService;

	@Pointcut("@annotation(org.jeecg.modules.bobo.common.aspect.annotation.LbAutoLog)")
	public void logPointCut() {

	}

	@Around("logPointCut()")
	public Object around(ProceedingJoinPoint point) throws Throwable {
		long beginTime = System.currentTimeMillis();
		//执行方法
		try {
			Object result = point.proceed();
			//执行时长(毫秒)
			long time = System.currentTimeMillis() - beginTime;
			//保存日志
			saveSysLog(point,result, time);
			return result;
		}catch (Exception e){
			autoServiceLogService.setLog( ((MethodSignature) point.getSignature()).getMethod()+"-error",JSONObject.toJSONStringWithDateFormat(point.getArgs(), JSON.DEFFAULT_DATE_FORMAT),e.getMessage());
			throw new BoboException(500, e.getMessage());
		}
	}
	@Async("lbAutoServiceLogServiceAsyncExecutor")
	public void saveSysLog(ProceedingJoinPoint joinPoint,Object result, long time) {
		LbAutoServiceLog lbAutoServiceLog=new LbAutoServiceLog();
		try {


		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
		lbAutoServiceLog.setCostTime((int) time);
		lbAutoServiceLog.setCreateTime(new DateTime());

		LbAutoLog syslog = method.getAnnotation(LbAutoLog.class);
		if(syslog != null){
			//注解上的描述,操作日志内容
			lbAutoServiceLog.setLogType(syslog.value());

		}
		//请求的方法名
		String className = joinPoint.getTarget().getClass().getName();
		String methodName = signature.getName();
		lbAutoServiceLog.setMethod(className + "." + methodName + "()");

		//请求的参数
		Object[] args = joinPoint.getArgs();

		String params = JSONObject.toJSONStringWithDateFormat(args, JSON.DEFFAULT_DATE_FORMAT);
		lbAutoServiceLog.setRequestParam(params);


		//获取request
		HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
		//设置IP地址
		lbAutoServiceLog.setIp(IPUtils.getIpAddr(request));
		lbAutoServiceLog.setLogContent(JSONObject.toJSONStringWithDateFormat(result, JSON.DEFFAULT_DATE_FORMAT));
		lbAutoServiceLog.setRequestType(request.getMethod());
		lbAutoServiceLog.setRequestUrl(request.getRequestURL().toString());
//		lbAutoServiceLog.setRequestParam(request.getQueryString());
		//获取登录用户信息获取
//		LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
//		if(sysUser!=null){
//			sysLog.setUser(sysUser.getUsername());
//		}
		autoServiceLogService.save(lbAutoServiceLog);
		}catch (Exception e){
			e.printStackTrace();
			lbAutoServiceLog.setLogType(lbAutoServiceLog.getLogType()+"-error");
			lbAutoServiceLog.setLogContent(e.getMessage());
			autoServiceLogService.save(lbAutoServiceLog);
		}
	}

}
